package com.spdb.spdbfl.utils;


import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;

public class FileUtil {

    /**
     * File转化为MultipartFile
     * @param file
     * @return
     */
    public static MultipartFile getMultipartFile(File file){
        FileInputStream fileInputStream = null;
        MultipartFile multipartFile = null;
        try {
            fileInputStream = new FileInputStream(file);
            multipartFile = new MultipartFileDto(file.getName(),file.getName(),
                    "text/plain",fileInputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return multipartFile;
    }

    /**
     * 远程file文件转MultipartFile
     * @param fileContentType
     * @param filePath
     * @return
     */
//    public static MultipartFile getMultipartFile(String fileContentType, String filePath) {
//        MultipartFile multipartFile = null;
//        try {
//            //文件转MultipartFile
//
//            URL url = new URL(filePath);
//            DataSource dataSource = new URLDataSource(url);
//            DataHandler dataHandler = new DataHandler(dataSource);
//            DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",
//                    MediaType.ALL_VALUE, true, dataHandler.getName());
//            IOUtils.copy(dataHandler.getInputStream(), fileItem.getOutputStream());
//            multipartFile = new CommonsMultipartFile(fileItem);
//        }catch (Exception e){
//            e.printStackTrace();
//            log.error("文件转MultipartFile失败-err:{}",e.getMessage());
//        }
//        return multipartFile;
//    }


}
